package cn.com.wxd.common.file;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.swing.plaf.basic.BasicInternalFrameTitlePane;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Title:读取配置文件信息
 * Description:cn.com.wxd.util
 * <p>
 * Company: Amumu管理平台
 * Copyright: Copyright (c) 2015
 * All right reserved.
 * Created time: 2016-8-25 下午12:04:39
 *
 * @author WangXuDong
 * @version 1.0
 */
public class ReadConfigFile {

    private static final Logger log = LoggerFactory.getLogger(ReadConfigFile.class);

    /**
     * 读取配置文件的信息
     *
     * @param fileName 文件名(默认从classpath跟目录向后扫描所有pachage，直到到jar包中，
     *                 如果文件名中包含了指定路径，那么只从给该路径中读取)
     * @param field    字段名称
     *
     * @return 如果未定义或者不存在则返回空字符串
     */
    public static String readPropertiesConfig(String fileName, String field) {
        String retStr;
        Configuration config;
        if (!fileName.contains(".properties")) {
            fileName = fileName + ".properties";
        }
        try {
            if(fileName.contains("/") || fileName.contains("\\")){
                config = new PropertiesConfiguration(new File(fileName));
            }else {
                config = new PropertiesConfiguration(fileName);
            }
            retStr = config.getString(field);
        } catch (Exception e) {
            log.error("Configuration File Read error:" + fileName, e);
            return "";
        }
        return retStr;
    }

    /**
     * 读取配置文件的信息
     *
     * @param fileName 文件名(默认从classpath跟目录向后扫描所有pachage，直到到jar包中，
     *                 如果文件名中包含了指定路径，那么只从给该路径中读取)
     * @param fields    字段名称数组
     *
     * @return 如果未定义或者不存在则返回空字符串
     */
    public static Map<String, String> readPropertiesConfig(String fileName) {
        Configuration config;
        Map<String, String> map = new HashMap<>();
        if (!fileName.contains(".properties")) {
            fileName = fileName + ".properties";
        }
        try {
            if(fileName.contains("/") || fileName.contains("\\")){
                config = new PropertiesConfiguration(new File(fileName));
            }else {
                config = new PropertiesConfiguration(fileName);
            }
        } catch (Exception e) {
            log.error("Configuration File Read error:" + fileName, e);
            return map;
        }
        Iterator<String> iterator = config.getKeys();
        while (iterator.hasNext()) {
            String key = iterator.next();
            map.put(key, config.getString(key));
        }
        return map;
    }

    /**
     * 输入一个正确的路径
     * 按行读取一个字符文件
     * @param maxLine
     * @return
     */
    public static List<String> readCharFilebyLine(String path){
        List<String> list = new ArrayList<>();
        BufferedReader br = null;
        try{
            File file = new File(path);
            if(file.isDirectory()){
                throw new IOException("Please specify character file, rather than a directory" + path);
            }
            br = new BufferedReader(new FileReader(new File(path)));//构造一个BufferedReader类来读取文件
            String s;
            while((s = br.readLine())!=null){//使用readLine方法，一次读一行
                list.add(s);
            }
        }catch(Exception e){
            log.error("Char FileRead error:" + path, e);
            return null;
        }finally {
            if(br !=null){
                try {
                    br.close();
                }catch (IOException e){
                    log.error("BufferedReader close Exception", e);
                }
            }
        }
        return list;
    }

    /**
     * 输入一个正确的路径
     * 按行读取一个字符文件
     * @param maxLine
     * @return
     */
    public static List<String> readCharFilebyLine(String path, int maxLine){
        List<String> list = new ArrayList<>();
        BufferedReader br = null;
        try{
            File file = new File(path);
            if(file.isDirectory()){
                throw new IOException("Please specify character file, rather than a directory" + path);
            }
            br = new BufferedReader(new FileReader(new File(path)));//构造一个BufferedReader类来读取文件
            String s;
            int linenum = 0;
            while((s = br.readLine())!=null){//使用readLine方法，一次读一行
                if(linenum >= maxLine){
                    break;
                }
                list.add(s);
                linenum++;
            }
        }catch(Exception e){
            log.error("Char FileRead error:" + path, e);
            return null;
        }finally {
            if(br !=null){
                try {
                    br.close();
                }catch (IOException e){
                    log.error("BufferedReader close Exception", e);
                }
            }
        }
        return list;
    }
}
